<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检测散列值是否匹配指定的选项</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.password-needs-rehash" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">password_needs_rehash</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7)</p><p class="refpurpose"><span class="refname">password_needs_rehash</span> &mdash; <span class="dc-title">检测散列值是否匹配指定的选项</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.password-needs-rehash-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>password_needs_rehash</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$algo</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   此函数检测指定的散列值是否实现了提供的算法和选项。
   如果没有，需要重新生成散列值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.password-needs-rehash-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<code class="parameter">hash</code></dt>

    <dd>

     <p class="para">
      一个由 <span class="function"><a href="password_hash.html" class="function">password_hash()</a></span> 创建的散列值。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">algo</code></dt>

    <dd>

     <p class="para">
      一个用来在散列密码时指示算法的<a href="password.constants.html" class="link">密码算法常量</a>。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">options</code></dt>

    <dd>

     <p class="para">
      一个包含有选项的关联数组。目前支持两个选项：<em>salt</em>，在散列密码时加的盐（干扰字符串），以及<em>cost</em>，用来指明算法递归的层数。这两个值的例子可在 <span class="function"><a href="crypt.html" class="function">crypt()</a></span> 页面找到。
     </p>
    </dd>

   
  </dl>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.password-needs-rehash-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="openssl_spki_export_challenge.example.keygen">
    <p><strong>Example #1 <span class="function"><strong>password_needs_rehash()</strong></span>用法</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hash&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'$2y$10$YCFsG6elYca568hBi2pZ0.3LDL5wjgxct1N8w/oLR/jfHsiQwCqTS'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;当硬件性能得到改善时，cost&nbsp;参数可以再修改<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'cost'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;根据明文密码验证储存的散列<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">password_verify</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$hash</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;检测是否有更新的可用散列算法<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;或者&nbsp;cost&nbsp;发生变化<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">password_needs_rehash</span><span style="color: #007700">(</span><span style="color: #0000BB">$hash</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PASSWORD_DEFAULT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;如果是这样，则创建新散列，替换旧散列<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$newHash&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PASSWORD_DEFAULT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;使用户登录<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p> 
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.password-needs-rehash-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果散列需要重新生成才能匹配指定的 <code class="parameter">algo</code> 和 <code class="parameter">options</code>，
   则返回 <strong><code>TRUE</code></strong>，否则返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

</div></div></div></body></html>